package com.zeedev.flyingsaucer.modules.BeerDatabaseModule.modules;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.beers.activities.beerlists.LocalListModel;
import com.zeedev.flyingsaucer.data.BeerUri;
import com.zeedev.flyingsaucer.modules.BeerDatabaseModule.impl.members.MemberBeerListModel;
import com.zeedev.flyingsaucer.modules.BeerDatabaseModule.interfaces.ListModel;
import mobi.zeedev.flyingsaucer.common.DataModule.BeerConstants;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv;
import mobi.zeedev.flyingsaucer.common.DataModule.Util;

/**
 * User: James
 * Date: 1/1/11
 * Time: 1:34 PM
 */
public class MemberModule extends AbstractModule {


    @Override
    protected void configure() {
        bind(ListModel.class).to(MemberBeerListModel.class);

    }

    @Provides
    @Named("memberDbId")
    Long getMemberDbId(ContentResolver mContentResolver, @Named("memberId") Integer memberId) {
        Long ret = null;
        Cursor q1 = mContentResolver.query(FlyingSaucerConv.Members.CONTENT_URI, Util.asAr(FlyingSaucerConv.Members.UFO_MEMBER_ID, FlyingSaucerConv.Members.MEMBERS_KEY), FlyingSaucerConv.Members.UFO_MEMBER_ID + "=?", Util.asAr(memberId), BaseColumns._ID);
        if (q1.moveToFirst()) {

            ret = q1.getLong(1);
        }
        q1.close();

        if (ret == null) {
            ContentValues cv = new ContentValues();
            cv.put(FlyingSaucerConv.Members.UFO_MEMBER_ID, memberId);
            Uri uriRet = mContentResolver.insert(FlyingSaucerConv.Members.CONTENT_URI, cv);
            ret = ContentUris.parseId(uriRet);
        }


        return ret;
    }

    @Provides
    BeerUri provideDefaultBeerUri(@Named("isTasted") Boolean tasted, @Named("memberId") Integer memberId) {

//		Builder b = MemberBeer.FILTER_CONTENT_URI.buildUpon();
//		b.appendQueryParameter(FlyingSaucerConv.SORT_BY, Beers.NAME + " ASC");
//
//		if (isTasted) {
//			b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "true");
//		} else {
//			b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "false");
//			b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
//		}

//        Boolean isTasted = this.isTasted();
        BeerUri uri = new BeerUri();
        uri.sortBy = FlyingSaucerConv.Beers.NAME + " asc";

        if (tasted) {
            uri.isTasted = true;
        } else {
            uri.isTasted = false;
            uri.isActive = true;
        }

        uri.memberId = memberId.toString();

        uri.addMemberBeer = true;

        return uri;
    }

    @Provides
    Cursor provideDefaultCursor(@Named("isTasted") Boolean tasted, Activity mActivity, @Named("memberId") Integer memberId) {
        Cursor cursor = null;
        Uri.Builder b = FlyingSaucerConv.Members.MemberBeer.FILTER_CONTENT_URI.buildUpon();

        b.appendQueryParameter(FlyingSaucerConv.SORT_BY, FlyingSaucerConv.Beers.NAME + " ASC");

        if (tasted) {
            b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "true");

        } else {
            b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "false");
            b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
        }
        cursor = mActivity.managedQuery(b.build(), Util.asArNonReduce(FlyingSaucerConv.Members.MemberBeer.STANDARD_COLUMNS, FlyingSaucerConv.Members.UFO_MEMBER_ID, FlyingSaucerConv.Members.MemberBeer.IS_TASTED, FlyingSaucerConv.Members.MemberBeer.ACTIVE), FlyingSaucerConv.Members.UFO_MEMBER_ID + "=?", Util.asAr(memberId), null);
        return cursor;
    }


    @Provides
    @Named("currentBeerUri")
    BeerUri provideCurrentBeerUri(LocalListModel model) {
        return model.getBeerUri();
    }

    @Provides
    @Named("currentCursor")
    Cursor provideCurrentCursor(Activity mActivity, @Named("currentBeerUri") BeerUri uri) {
        Cursor cursor = null;
//        Activity act = (Activity) controller.getParent();
//		Builder b = Members.MemberBeer.FILTER_CONTENT_URI.buildUpon();
//
//		b.appendQueryParameter(FlyingSaucerConv.SORT_BY, Beers.NAME + " ASC");
//
//		if (isTasted()) {
//			b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "true");
//		} else {
//			b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "false");
//			b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
//		}

        Uri actualUri = uri.getUri(FlyingSaucerConv.Members.MemberBeer.FILTER_CONTENT_URI);

        cursor = mActivity.managedQuery(actualUri, Util.asArNonReduce(FlyingSaucerConv.Members.MemberBeer.STANDARD_COLUMNS, FlyingSaucerConv.Members.MemberBeer.MEMBER_BEER_KEY), null, null, null);
        return cursor;
    }

    @Provides
    @Named("groupBy")
    String providesGroupBy(Activity mActivity) {
        return mActivity.getIntent().getStringExtra(BeerConstants.INTENT_BEERTREE_MODE);
    }

    @Provides
    @Named("treeCursor")
    Cursor getTreeCursor(Activity mActivity, @Named("groupBy") String groupBy, @Named("memberId") Integer memberId, @Named("originDate") String originDate) {
        Cursor ret = null;

        if (groupBy.equals(FlyingSaucerConv.Beers.STYLE_NAME)) {

//			Uri styles = Beers.Styles.CONTENT_URI;
            //mCursor = cr.query(beers, MetaBeer.BEER_LIST_COLUMNS, null, null, );
            Uri.Builder b = FlyingSaucerConv.Members.MemberBeer.FILTER_CONTENT_URI.buildUpon();
            b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "true");
//			b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
            b.appendQueryParameter(FlyingSaucerConv.GROUP_BY, groupBy);
            b.appendQueryParameter(FlyingSaucerConv.SORT_BY, groupBy + " asc");

            ret = mActivity.managedQuery(
                    b.build(),
                    Util.asArNonReduce(groupBy, FlyingSaucerConv.Members.MemberBeer.STANDARD_COLUMNS, FlyingSaucerConv.Members.UFO_MEMBER_ID, FlyingSaucerConv.Members.MemberBeer.IS_TASTED, FlyingSaucerConv.Members.MemberBeer.ACTIVE),
                    FlyingSaucerConv.Members.UFO_MEMBER_ID + "=?",
                    Util.asAr(memberId),
                    null
            );

        } else if (groupBy.equals(FlyingSaucerConv.Beers.BREWER_NAME)) {

            Uri.Builder b = FlyingSaucerConv.Members.MemberBeer.FILTER_CONTENT_URI.buildUpon();
            b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "true");
//			b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
            b.appendQueryParameter(FlyingSaucerConv.GROUP_BY, groupBy);

            String[] t2 = Util.asArNonReduce(groupBy, FlyingSaucerConv.Members.MemberBeer.STANDARD_COLUMNS, FlyingSaucerConv.Members.UFO_MEMBER_ID, FlyingSaucerConv.Members.MemberBeer.IS_TASTED, FlyingSaucerConv.Members.MemberBeer.ACTIVE);

            ret = mActivity.managedQuery(
                    b.build(),
                    t2,
                    FlyingSaucerConv.Members.UFO_MEMBER_ID + "=? AND (NOT " + groupBy + " ISNULL)",
                    Util.asAr(memberId.toString()),
                    groupBy + " asc");
        } else if (groupBy.equals(FlyingSaucerConv.Members.MemberBeer.SIMPLE_CREATION_DATE)) {
            Uri.Builder b = FlyingSaucerConv.Members.MemberBeer.FILTER_CONTENT_URI.buildUpon();
            b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "false");
            b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
            b.appendQueryParameter(FlyingSaucerConv.GROUP_BY, groupBy);
            b.appendQueryParameter(FlyingSaucerConv.SORT_BY, FlyingSaucerConv.Members.MemberBeer.CREATION_DATE + " desc");

            ret = mActivity.managedQuery(
                    b.build(),
                    Util.asArNonReduce(groupBy, FlyingSaucerConv.Members.MemberBeer.STANDARD_COLUMNS, FlyingSaucerConv.Members.UFO_MEMBER_ID, FlyingSaucerConv.Members.MemberBeer.IS_TASTED, FlyingSaucerConv.Members.MemberBeer.ACTIVE, FlyingSaucerConv.Members.MemberBeer.SIMPLE_CREATION_DATE, FlyingSaucerConv.Members.MemberBeer.CREATION_DATE),
                    FlyingSaucerConv.Members.UFO_MEMBER_ID + "=? and " + FlyingSaucerConv.Members.MemberBeer.CREATION_DATE + "!= ?",
                    Util.asAr(memberId, originDate),
                    null//groupBy + " ASC"
            );
        } else if (groupBy.equals(FlyingSaucerConv.Members.MemberBeer.SIMPLE_TASTED_DATE)) {
            Uri built = null;
            Uri.Builder b = FlyingSaucerConv.Members.MemberBeer.FILTER_CONTENT_URI.buildUpon();
            b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "true");
//			b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
            b.appendQueryParameter(FlyingSaucerConv.GROUP_BY, groupBy);
            b.appendQueryParameter(FlyingSaucerConv.SORT_BY, FlyingSaucerConv.Members.MemberBeer.TASTED_DATE + " desc");

            ret = mActivity.managedQuery(
                    b.build(),
                    Util.asArNonReduce(groupBy, FlyingSaucerConv.Members.MemberBeer.STANDARD_COLUMNS, FlyingSaucerConv.Members.UFO_MEMBER_ID, FlyingSaucerConv.Members.MemberBeer.IS_TASTED, FlyingSaucerConv.Members.MemberBeer.ACTIVE, FlyingSaucerConv.Members.MemberBeer.SIMPLE_TASTED_DATE, FlyingSaucerConv.Members.MemberBeer.TASTED_DATE),
                    FlyingSaucerConv.Members.UFO_MEMBER_ID + "=?",// and " + MemberBeer.IS_TASTED + "=1 and " + MemberBeer.ACTIVE + "=1",
                    Util.asAr(memberId),
                    null
            );

        }
/*		ret2 = act.managedQuery(
				Members.MemberBeer.CONTENT_URI,
				null,
				MemberBeer.IS_TASTED + "=1",
				null,
				null//groupBy + " ASC"
			);
		Log.d(TAG,"origin date:" + this.getOriginDate());
		DatabaseUtils.dumpCursor(ret);
		ret2.moveToFirst();
		for (int i = 0; i < 5; i++) {
			DatabaseUtils.dumpCurrentRow(ret2);
			ret2.moveToNext();
		}*/
        return ret;
    }

    @Provides
    @Named("originDate")
    String getOriginDate(ContentResolver mContentResolver, @Named("memberId") Integer memberId) {
        String ret = null;

        Cursor q1 = mContentResolver.query(FlyingSaucerConv.Members.CONTENT_URI, Util.asAr(FlyingSaucerConv.Members.UFO_ORIGIN_DATE, FlyingSaucerConv.Members.UFO_MEMBER_ID, FlyingSaucerConv.Members.MEMBERS_KEY), FlyingSaucerConv.Members.UFO_MEMBER_ID + "=?", Util.asAr(memberId), BaseColumns._ID);
        if (q1.moveToFirst()) {

            ret = q1.getString(q1.getColumnIndex(FlyingSaucerConv.Members.UFO_ORIGIN_DATE));
        }
        q1.close();
        return ret;
    }

    @Provides
    @Named("isTasted")
    Boolean providesIsTasted(Activity mActivity) {
        return mActivity.getIntent().getBooleanExtra(BeerConstants.IS_TASTED_LIST, false);
    }

    @Provides
    @Named("isDatabaseEmpty")
    Boolean isEmpty(ContentResolver mContentResolver, @Named("memberId") Integer memberId) {
        Boolean ret = true;

        //Cursor c = this.mContentResolver.query(Members.MemberBeer.CONTENT_URI, Util.asAr(MemberBeer.MEMBER_BEER_KEY, Members.UFO_MEMBER_ID), Members.UFO_MEMBER_ID+"=?", Util.asAr(this.getMemberDbId(memberId)), BaseColumns._ID);
        Cursor c = null;
        try {
            Uri q1 = FlyingSaucerConv.Members.MemberBeer.FILTER_CONTENT_URI;
            Uri.Builder b = q1.buildUpon();
            b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
//			b.appendQueryParameter(Members.UFO_MEMBER_ID, memberId.toString());
            c = mContentResolver.query(
                    b.build(),
                    Util.asAr(FlyingSaucerConv.Members.MemberBeer.MEMBER_BEER_KEY, FlyingSaucerConv.Members.UFO_MEMBER_ID, FlyingSaucerConv.Members.MemberBeer.ACTIVE),
                    FlyingSaucerConv.Members.UFO_MEMBER_ID + "=?",
                    Util.asAr(memberId),
                    null
            );

            ret = c.getCount() <= 0;
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return ret;
    }


}
